﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace Sharp.Portal.Mvc.Form
{
    /// <summary>
    /// 模块项字段信息
    /// </summary>
    public partial class SharpFrom<TEntity>
    {
        /// <summary>
        /// 初始化界面
        /// 在派生类中重载
        /// </summary>
        protected virtual void InitForm()
        {
        }
        /// <summary>
        /// 初始化表单字段
        /// </summary>
        public void InitializeFormFields()
        {
            InitForm();
        }
         
        /// <summary>
        /// 小节列表
        /// </summary>
        public List<FormSection> Sections { get; set; }

        #region 增加小节
        /// <summary>
        /// 增加小节
        /// </summary>
        /// <param name="title">小节名称</param>
        /// <param name="iFrameUri">iFrame 链接，当这个参数不为空时，禁止加入子元素</param>
        /// <returns></returns>
        protected FormSection AddSection(string title, string iFrameUri = "")
        {
            FormSection item = new FormSection(title, iFrameUri);
            Sections.Add(item);
            return item;
        }
        #endregion

        

        #region 新增表单内容

     
        /// <summary>
        /// 新增表单内容
        /// </summary> 
        /// <param name="path"></param>
        /// <returns>表单内容对象</returns>
        public FieldItem AddFormContent<TProperty>(Expression<Func<TEntity, TProperty>> path)
        {
            FieldItem field = new FieldItem();
            string por;
            if (LambdaHelper.TryParsePath(path.Body, out por))
            {
                field.Name = por;
            }

            string display;
            if (LambdaHelper.TryParseDisplay(path.Body, out display))
            {
                field.FieldLabel = display;
            }
            string masterDataCategory;
            if (LambdaHelper.TryParseCodeSort(path.Body, out masterDataCategory))
            {
                field.MasterDataCategory = masterDataCategory;
            }
             
            return field;
        }
      
        #endregion
        
    }
}
